<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="initial-scale=1,maximum-scale=1, minimum-scale=1">
    <title>物资报废</title>
    <link rel="stylesheet" href="css/public.css">
    <style>
    .my_project {
        padding-top: 0.66rem;
    }

    .my_project button {
        width: 5.46rem;
        height: 0.86rem;
        margin: 0 auto;
        position: relative;
        background: #3D88DD;
        display: block;
        color: white;
        font-size: 0.3rem;
        border-radius: 0.2rem;
        box-shadow: 0px 2px 4px 1px #4994e7bd;
        border: none;
        margin-top: 0.58rem;
        margin-bottom: 0.8rem;
    }
    </style>
</head>

<body>
    <div class="my_project">
        <div class="card_content" style="height:8.0rem;width: 6.8rem;">
            <div class="card_tittle">
                <span style="margin-left:0.24rem;margin-right: 0.4rem">名称</span>
                <span style="margin-right: 0.7rem">规格型号</span>
                <span style="margin-right: 0.4rem">采购时间</span>
                <span style="margin-right: 0.4rem">数量</span>
                <span>使用数量</span>
            </div>
            <hr>
            <ul class="my-project">

            </ul>
        </div>
        <button class="save-num">保存</button>
    </div>
    <script type="text/javascript" src="js/public.js"></script>
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="application/javascript">
        function getQueryString(name) {

            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        }
        var oAccessToken = getQueryString('access-token');


        /*物资数量+1*/
    function numAdd() {
        var quantity = document.getElementById("quantity").value;
        var num_add = parseInt(quantity) + 1;
        if (quantity == "") {
            num_add = 1;
        }

        document.getElementById("quantity").value = num_add;
        console.log(num_add);
    }
    /*物资数量-1*/
    function numDec() {
        var quantity = document.getElementById("quantity").value;
        var num_dec = parseInt(quantity) - 1;
        if (quantity == "" || quantity <= 0) {
            num_dec = 0;
        }
        document.getElementById("quantity").value = num_dec;
    }
    $('.save-num').click(function(){
        var aList = $('.my_ml_list');
        var aNum  = $('.quantity');
        for(var i=0;i<aList.length;i++){
            if( $(aNum[i]).val() != 0 ){
                var Id  = $(aList[i]).attr('id');
                var Num = $(aNum[i]).val();
            }
        }
        $.ajax({
            url:'/v1/goods/num?access-token='+oAccessToken,
            type:'post',
            data:{
                id:Id,
                use_num:Num,
            },
            success:function(data){
                if(data.status == 200){
                    window.location.href = './my_project.html?access-token='+oAccessToken;
                }else{
                    alert(data.msg)
                }
            },
            error:function(data){
                if(data.status == 401){
                    window.location.href = './index.html';
                }
            }
        })
    })

    $.ajax({
        url:"/v1/goods/my-project?access-token="+oAccessToken,
        type:'post',
        data:'',
        async:true,
        success:function(data){
            console.log(data)
            var len = data.body.length;
            if(data.status == 200){
                var html='';
                for(let i=0;i<len;i++){
                    html += '<li class="my_ml_list" id='+data.body[i].id+'><span>'+data.body[i].name+'</span><span class="model"> <p>'+data.body[i].scale+'</p><p>'+data.body[i].type+'</p></span>'+
                        '<span style="margin-left: 0.28rem">'+data.body[i].buy_time+'</span><span class="my_product_num" style="margin-left: 0.68rem">'+data.body[i].use_num+'/'+data.body[i].num+'</span>'+
                        '<span class="add_reduce"> <span onclick="numDec(this)"><img style="width:0.2rem;margin-left: 0.1rem"  src="./images/reduce.png"></span>'+
                        '<input style="text-align: center;" type="text" class="quantity" value="0" /><span onclick="numAdd(this)"><img style="width:0.2rem;margin-right: 0.1rem;"  src="./images/add.png"></span></span>'+
                        '<hr style="width: 100%;"></li>';
                }
                $('.my-project').html(html);
            }
        },
        error:function(data){
            if(data.status == 401){
                window.location.href = './index.html';
            }
        },
    });


    </script>
</body>

</html>